package com.atguigu.jxc.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.SellService;
import org.omg.CORBA.ObjectHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping
public class SellController {
    @Autowired
    private SellService sellService;
    /**
     * 请求URL：http://localhost:8080/customer/getComboboxList
     * 请求参数：String q（客户名称）
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：List<Customer>
     */
    @PostMapping("customer/getComboboxList")
    public List<Customer> getCustomerList(String q){
        return sellService.getCustomerList(q);
    }
    /**
     * 请求URL：http://localhost:8080/saleListGoods/save?saleNumber=XS1605772263999 （销售单号）
     * 请求参数：SaleList saleList, String saleListGoodsStr
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     */
    @PostMapping("saleListGoods/save")
    public ServiceVO saveSaleListGoods(SaleList saleList, String saleListGoodsStr, HttpSession session){
        try {
            User user = (User) session.getAttribute("currentUser");
            saleList.setUserId(user.getUserId());
            List<SaleListGoods> saleListGoods = JSON.parseArray(saleListGoodsStr, SaleListGoods.class);
            sellService.saveSaleListGoods(saleList,saleListGoods);
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(200,"请求失败");
        }
        return new ServiceVO(100,"请求成功");
    }
    /**
     * 请求URL：http://localhost:8080/customerReturnListGoods/save?returnNumber=XT1605772786495（退货单号）
     * 请求参数：CustomerReturnList customerReturnList, String customerReturnListGoodsStr
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     */
    @PostMapping("customerReturnListGoods/save")
    public ServiceVO saveCustomerReturnListGoods(CustomerReturnList customerReturnList, String customerReturnListGoodsStr){
        try {
            List<CustomerReturnListGoods> list = JSON.parseArray(customerReturnListGoodsStr, CustomerReturnListGoods.class);
            sellService.saveCustomerReturnListGoods(customerReturnList,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(200,"请求失败");
        }
        return new ServiceVO(100,"请求成功");
    }
    /**
     * 请求URL：http://localhost:8080/saleListGoods/list
     * 请求参数：String saleNumber, Integer customerId, Integer state, String sTime,String eTime
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：Map<String,Object>
     */
    @PostMapping("saleListGoods/list")
    public Map<String,Object> getSaleListGoodsList(String saleNumber, Integer customerId, Integer state, String sTime,String eTime){
        return sellService.getSaleListGoodsList(saleNumber,customerId,state,sTime,eTime);
    }
    /**
     * 请求URL：http://localhost:8080/customerReturnListGoods/list
     * 请求参数：String returnNumber, Integer customerId, Integer state, String sTime,String eTime
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：Map<String,Object>
     */
    @PostMapping("customerReturnListGoods/list")
    public Map<String,Object> getCustomerReturnListGoodsList(String returnNumber, Integer customerId, Integer state, String sTime,String eTime){
        return sellService.getCustomerReturnListGoodsList(returnNumber,customerId,state,sTime,eTime);
    }
    /**
     * 请求URL：http://localhost:8080/saleListGoods/goodsList
     * 请求参数：Integer saleListId
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：Map<String,Object>
     */
    @PostMapping("saleListGoods/goodsList")
    public Map<String, Object> saleGoodsList(Integer saleListId){
        return sellService.saleGoodsList(saleListId);
    }
    /**
     * 请求URL：http://localhost:8080/customerReturnListGoods/goodsList
     * 请求参数：Integer customerReturnListId
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：Map<String,Object>
     */
    @PostMapping("customerReturnListGoods/goodsList")
    public Map<String, Object> customerReturnListGoodsList(Integer customerReturnListId){
        return sellService.customerReturnListGoodsList(customerReturnListId);
    }
    /**
     * 请求URL：http://localhost:8080/saleListGoods/delete
     * 请求参数：Integer saleListId
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     */
    @PostMapping("saleListGoods/delete")
    public ServiceVO delSaleListAndGodos(Integer saleListId){
        sellService.delSaleListAndGodos(saleListId);
        return new ServiceVO(100,"请求成功!!");
    }
    /**
     * 请求URL：http://localhost:8080/customerReturnListGoods/delete
     * 请求参数：Integer customerReturnListId
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     */
    @PostMapping("customerReturnListGoods/delete")
    public ServiceVO delCustomerReturnListAndGoods(Integer customerReturnListId){
        try {
            sellService.delCustomerReturnListAndGoods(customerReturnListId);
            return new ServiceVO(100,"请求成功!!");
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(200,"请求失败!!");
        }
    }
    /**
     * 请求URL：http://localhost:8080/saleListGoods/updateState
     * 请求参数：Integer saleListId
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     */
    @PostMapping("saleListGoods/updateState")
    public ServiceVO updateState(Integer saleListId){
        try {
            sellService.updateState(saleListId);
            return new ServiceVO(100,"请求成功!!");
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(200,"请求失败!!");
        }
    }
    /**
     * 请求URL：http://localhost:8080/saleListGoods/count
     * 请求参数：String sTime, String eTime ,Integer goodsTypeId, String codeOrName
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：String
     */
    @PostMapping("saleListGoods/count")
    public List<Map> saleCount(String sTime, String eTime ,Integer goodsTypeId, String codeOrName){
        return sellService.count(sTime,eTime,goodsTypeId,codeOrName);
    }
    /**
     * 请求URL：http://localhost:8080/saleListGoods/getSaleDataByDay
     * 请求参数：String sTime, String eTime
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：String
     * 用户点击左侧按日统计分析菜单时，
     * 右侧展示所选日期区间的日销售总额、成本总额、
     * 盈利总额数据列表，上方采用柱状图形式展示，
     */
    @PostMapping("saleListGoods/getSaleDataByDay")
    public List<AVGPojo> getSaleDataByDay(String sTime, String eTime){
        return sellService.getSaleDataByDay(sTime,eTime);
    }
    /**
     * 请求URL：http://localhost:8080/saleListGoods/getSaleDataByMonth
     * 请求参数：String sTime, String eTime
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：String
     * 用户点击左侧按月统计分析菜单时，
     * 右侧展示所选日期区间的月销售总额、成本总额、
     * 盈利总额数据列表，上方采用柱状图形式展示
     */
    @PostMapping("saleListGoods/getSaleDataByMonth")
    public List<AVGPojo> getSaleDataByMonth(String sTime, String eTime){
        return sellService.getSaleDataByMonth(sTime,eTime);
    }
}